 var QQMapWX = require('../qqmapwxjssdk/qqmap-wx-jssdk.js');
//  var qqmapsdk = null as any;
import User from '../../store/user'
import * as api from '../../api/index'
import * as userutil from '../../store/userutil'
var qqmaputil = {} as any


let qqkey = 'WQYBZ-EM2KQ-XJI5C-2TXIR-5FPJZ-TIBXF'

///位置逆地理转换
qqmaputil.userReverseGeocoder= (lat:any,lng:any,complete:any) =>{

  var qqmapsdk = new QQMapWX({
    key: qqkey
  });
  qqmapsdk.reverseGeocoder({
    location: {
      latitude: lat,
      longitude: lng
    },
    
    success: (res:any) => {

      console.log(' 逆地理解析结果'+JSON.stringify(res.result));

      let adcode = '';
      if(res.result.ad_info.adcode.length === 6){
        adcode =  res.result.ad_info.adcode.slice(0,4) + '00'
      }else{
        adcode = res.result.ad_info.adcode
      }

      let addr = {title:res.result.address,
                  address:res.result.address,  
                  lat:res.result.location.lat,    
                  lng:res.result.location.lng,    
                  city:res.result.ad_info.city,    
                  citycode:adcode,         
      } as any;

      complete(addr)
    },
    fail: function(error:any) {
      console.error('逆地理报错：'+JSON.stringify(error));
    },
    complete: function(res:any) {
      console.log('逆地理完成：'+res);
    }


  })

}


///开启持续位置更新监听
qqmaputil.userLocateChangeListener= (callback:any) =>{

  var qqmapsdk = new QQMapWX({
    key: qqkey
});

  uni.onLocationChange(function (res:any) {

    console.log('定位更新：'+JSON.stringify(res));
   

    callback()
  
  });

  uni.startLocationUpdate({
      success: res => {
        
      },
      fail: err =>{
        console.error('开启小程序接收位置消息失败：', err)
      },
      complete: msg =>{
          console.log('调用开启小程序接收位置消息 API 完成')
      }
  });

}



//清路径搜索
qqmaputil.poiSearch = (key:any,complete:Function) => {
  var _this = this;

  var qqmapsdk = new QQMapWX({
      key: qqkey
  });
  // 调用接口
  qqmapsdk.search({
     keyword: key,  //搜索关键词
    //  location: '39.980014,116.313972',  //设置周边搜索中心点
     success: function (res:any) { //搜索成功后的回调


       complete(res.data)
     },
     fail: function (res:any) {
       console.log(res);
     },
     complete: function (res:any){
       console.log(res);
     }
 });
}


//清路径搜索
qqmaputil.routeplan = (startlat:number,startlon:number,endlat:number,endlon:number,complete:Function) => {
     // 调用接口
     var _this = this;

     var qqmapsdk = new QQMapWX({
          key: qqkey
      });

    //调用距离计算接口
    qqmapsdk.direction({
      mode: 'driving',//可选值：'driving'（驾车）、'walking'（步行）、'bicycling'（骑行），不填默认：'driving',可不填
      //from参数不填默认当前地址
      from: {
        latitude: startlat,
        longitude: startlon
      },
      to: {
        latitude: endlat,
        longitude: endlon
      }, 
      success: function (res:any) {
        // console.log(res);
        var ret = res;
        var coors = ret.result.routes[0].polyline, pl = [];
        //坐标解压（返回的点串坐标，通过前向差分进行压缩）
        var kr = 1000000;
        for (var i = 2; i < coors.length; i++) {
          coors[i] = Number(coors[i - 2]) + Number(coors[i]) / kr;
        }
        //将解压后的坐标放入点串数组pl中
        for (var i = 0; i < coors.length; i += 2) {
          pl.push({ latitude: coors[i], longitude: coors[i + 1] })
        }
      

        var mapRes = {
          latitude:pl[0].latitude,
          longitude:pl[0].longitude,
          polyline: [{
            points: pl,
            arrowLine: true,
            // arrowIconPath:'/static/img/custtexture_green.png',
            color:'#03C96D',
            width: 12
          }]
        }

        complete(mapRes)
      },
      fail: function (error:any) {
        console.error('路径规划报错：'+JSON.stringify(error));
      },
      complete: function (res:any) {
        // console.log('路径规划完成：'+JSON.stringify(res));
      }
    });
}


 
export default qqmaputil